//Documentation complete
/**
 * @author Kazu
 * @version 2009MR22
 */

package problemDomain;

import static problemDomain.ErrorBean.throwError;
import static problemDomain.ErrorBean.ErrorCode.*;
import static utilities.XMLTools.encodeEntities;

import java.util.Iterator;

/**
 * An item in a food order.
 */
public class OrderItemBean extends MenuItemBean {
	
	/* Constants */
	
	private static final long serialVersionUID = -3952360476874405212L;
	
	/* END Constants */
	
	
	/* Attributes */
	
	private int quantity;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs an <code>OrderItemBean</code> with default values.
	 */
	public OrderItemBean () {
		super();
		quantity = 0;
	}
	
	/**
	 * Constructs an <code>OrderItemBean</code> with specified values.
	 * @param menuItem the MenuItem it build this OrderItem from
	 * @param quantity the quantity of the MenuItem
	 */
	public OrderItemBean (MenuItemBean menuItem, int quantity) {
		this.itemList = menuItem.itemList;
		this.id = menuItem.id;
		this.name = menuItem.name;
		this.number = menuItem.number;
		this.price = menuItem.price;
		this.quantity = quantity;
	}
	
	/* END Constructors */

	/* Accessors */
	
	/**
	 * Returns the quantity of the item.
	 * @return the quantity of the item
	 */
	public int getQuantity() {
		return quantity;
	}
	
	/* END Accessors */
	
	
	/* Mutators */
	
	/**
	 * Sets the the quantity of the item.
	 * @param quantity the quantity of the item to set
	 */
	public void setQuantity (int quantity) {
		this.quantity = quantity;
	}
	
	/* END Mutators */
	
	
	/* Methods */
	
	/**
	 * Return the total price of the order item. 
	 * @return the total price of the order item
	 */
	public double getTotal () {
		return getPrice() * quantity;
	}
	
	/* END Methods */
	
	@Override
	public boolean isValid () throws ErrorBean {
		super.isValid();
		
		if (quantity < 0)
			throwError(
				ORDERITEM_QUANTITY_INVALID,
				String.valueOf(price),
				this);
		
		return true;
	}
	
	@Override
	public String toXML () {
		StringBuilder sb = new StringBuilder();
		sb.append("<orderitem>");
		sb.append("<id>" + id + "</id>");
		if (menu != null)
			sb.append(menu.toXML());
		else
			sb.append("<menu></menu>");
		sb.append("<name>" + (name != null ? name : "") + "</name>");
		sb.append("<price>" + price + "</price>");
		sb.append("<number>" + number + "</number>");
		sb.append("<quantity>" + quantity + "</quantity>");
		sb.append("<total>" + getTotal() + "</total>");
		sb.append("<fooditems>");
		if (itemList != null) {
			Iterator<FoodItemBean> i = itemList.iterator();
			while (i.hasNext())
				sb.append(i.next().toXML());
		}
		sb.append("</fooditems>");
		sb.append("</orderitem>");
		return encodeEntities(sb.toString());
	}
	
	/* END (Bean) Methods */
	
	
	/* (Object) Methods */
	
	@Override
	public String toString () {
		return super.toString() +
			   "\nQuantity: " + quantity + '\n';
	}
	
	/* END (Object) Methods */
	
}
